package com.mo.ld.activity.gate

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Build
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.chad.library.adapter.base.viewholder.BaseViewHolder

import com.mo.ld.R
import com.mo.lib.utils.*
import com.mo.lib.adapter.BaseRvAdapter
import org.litepal.LitePal

/**
 * @ author：mo
 * @ data：2022/4/14:13:41
 * @ 功能：
 */
fun getAdapterBalances(activity: Activity): BaseRvAdapter<GateCoins> {
    return object : BaseRvAdapter<GateCoins>(R.layout.item_gate_list) {
        @RequiresApi(Build.VERSION_CODES.N)
        override fun doWhat(holder: BaseViewHolder, bean: GateCoins, position: Int) {
            val view = holder.getView<TextView>(R.id.tv_item_cion_list)
            SpannableStringUtil().add(bean.key) { setSize(10) }.add("\n" + bean.value) { setSize(7) }.inToView(view)
            holder.itemView.setClick {
                startActivity<GateDeatilActivity>(bean.key + "_usdt")
            }
        }
    }
}

fun getAdapterOpenOrders(activity: Activity): BaseRvAdapter<Order> {
    return object : BaseRvAdapter<Order>(R.layout.item_gate_list) {
        @RequiresApi(Build.VERSION_CODES.N)
        override fun doWhat(holder: BaseViewHolder, bean: Order, position: Int) {
            val view = holder.getView<TextView>(R.id.tv_item_cion_list)
            SpannableStringUtil().add(bean.currencyPair.split("_")[0].uppercase()) { setSize(10) }.add("\n" + bean.rate) { setSize(7) }.inToView(view)
            holder.itemView.setClick {
                startActivity<GateDeatilActivity>(bean.currencyPair)
            }
        }
    }
}

/** 收藏适配器*/
fun getAdapterCollectList(activity: Activity, onLongClick: (String.() -> Unit)? = null): BaseRvAdapter<GateCollectCoins> {
    return object : BaseRvAdapter<GateCollectCoins>(R.layout.item_gate_list, LitePal.findAll(GateCollectCoins::class.java)) {
        @RequiresApi(Build.VERSION_CODES.N)
        override fun doWhat(holder: BaseViewHolder, bean: GateCollectCoins, position: Int) {
            val view = holder.getView<TextView>(R.id.tv_item_cion_list)
            SpannableStringUtil().add(bean.name.split("_")[0]) { setSize(13) }.inToView(view)
            holder.itemView.setClick {
                startActivity<GateDeatilActivity>(bean.name+"_USDT")
            }
            holder.itemView.setLongClick {
                LitePal.deleteAll(GateCollectCoins::class.java, "name = ?", bean.name)
                setNewInstance(LitePal.findAll(GateCollectCoins::class.java))
            }
        }
    }
}

/** 所有适配器*/
fun getAdapterAllCionList(activity: Activity, adapterCollect: BaseRvAdapter<GateCollectCoins>): BaseRvAdapter<GateCoins> {
    return object : BaseRvAdapter<GateCoins>(R.layout.item_gate_list, LitePal.findAll(GateCoins::class.java)) {
        @SuppressLint("SetTextI18n")
        @RequiresApi(Build.VERSION_CODES.N)
        override fun doWhat(holder: BaseViewHolder, bean: GateCoins, position: Int) {
            val view = holder.getView<TextView>(R.id.tv_item_cion_list) //            view.text = bean.name.split("_")[0] + "=" + bean.lockSize
            SpannableStringUtil().add(bean.key) { setSize(10) }.add(if (bean.balance == 0.0) "" else "\n" + bean.balance.toString()) {
                    setSize(7)
                }.inToView(view)
            holder.itemView.setClick {
                startActivity<GateDeatilActivity>(bean.key+"_USDT")
            }
            holder.itemView.setLongClick {
                val find = LitePal.where("name = ?", bean.key).find(GateCollectCoins::class.java)
                if (find.isNullOrEmpty()) {
                    val collect = GateCollectCoins(bean.key)
                    collect.save()
                    adapterCollect.setNewInstance(LitePal.findAll(GateCollectCoins::class.java))
                }

            }
        }
    }
}

